package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.HideProInfoAddActivity;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.StringUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.sg.CYbgcInfo;
import com.hongxun.greenentity.sg.CBasicsWorkEntity;
import com.hongxun.greenentity.sg.CGroundEntity;
import com.hongxun.greenentity.sg.CLinePitEntity;
import com.hongxun.greenentity.sg.CPolePitEntity;
import com.hongxun.interfaces.ClickListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 隐蔽工程信息列表
 */
public class EditHideProInfoList extends BaseListFragment {
    private List<CYbgcInfo> beanList = new ArrayList<>();
    private SuperAdapter<CYbgcInfo> superAdapter;

    public List<CYbgcInfo> getBeanList() {
        return beanList;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CYbgcInfo>(R.layout.sg_content_mclistadd_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, CYbgcInfo bean, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    Object ssUrl = SgDBUtils.getSsUrl(bean.getUuid());
                    XUtils.loadImg(context, ssUrl, imageView);
                    ((TextView) viewHolder.getView(R.id.tvl2)).setText(bean.getTvl2());
                    ((TextView) viewHolder.getView(R.id.tvl3)).setText(bean.getTvl3());
                    ((TextView) viewHolder.getView(R.id.tvl4)).setText(bean.getTvl4());
                    ((TextView) viewHolder.getView(R.id.tvr1)).setText(bean.getName());
                    ((TextView) viewHolder.getView(R.id.tvr2)).setText(StringUtils.getNull(bean.getTvr2()));
                    ((TextView) viewHolder.getView(R.id.tvr3)).setText(StringUtils.getNull(bean.getTvr3()));
                    ((TextView) viewHolder.getView(R.id.tvr4)).setText(StringUtils.getNull(bean.getTvr4()));
                    ((TextView) viewHolder.getView(R.id.tvr5)).setText(StringUtils.getNull(bean.getTvr5()));
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(beanList);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CYbgcInfo>() {
            @Override
            public void onClick(int i, CYbgcInfo bean) {
                SPUtils.put(context, SgStrKey.uuidxx, bean.getUuid());
                startActivity(new Intent(context, HideProInfoAddActivity.class)
                        .putExtra("isNewAdd", false)
                        .putExtra("itemName", bean.getName()));
            }
        });
        superAdapter.setOnItemLongClickListener(new SuperAdapter.OnItemLongClickListener<CYbgcInfo>() {
            @Override
            public void onLongClick(int i, CYbgcInfo bean) {
                longClickDel(bean);
            }
        });
        return superAdapter;
    }

    //删除项目
    private void longClickDel(final CYbgcInfo bean) {
        new NoticeDialog()
                .setTitle("删除记录")
                .setMsg(bean.getName())
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        beanList.remove(bean);
                        superAdapter.notifyDataSetChanged();
                        delLocalData(bean);
                    }
                }).show(getFragmentManager(), "");
    }

    private void delLocalData(CYbgcInfo bean) {
        if (bean.getName().equals("电杆坑")) {
            SgDBUtils.delCPolePitEntity(bean.getUuid());
        } else if (bean.getName().equals("拉线坑")) {
            SgDBUtils.delCLinePitEntity(bean.getUuid());
        } else if (bean.getName().equals("接地体")) {
            SgDBUtils.delCGroundEntity(bean.getUuid());
        } else if (bean.getName().equals("设备基础")) {
            SgDBUtils.delCBasicsWorkEntity(bean.getUuid());
        }
        T.showShort(context, "删除成功");
    }

    @Override
    public void onRefresh() {
        Observable.just(SgStrKey.uuidjl).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                beanList.clear();
                String uuidJl = String.valueOf(SPUtils.get(context, value, ""));
                List<CBasicsWorkEntity> list1 = SgDBUtils.getCBasicsWorkList(uuidJl);
                List<CGroundEntity> list2 = SgDBUtils.getCGroundList(uuidJl);
                List<CPolePitEntity> list3 = SgDBUtils.getCPolePitList(uuidJl);
                List<CLinePitEntity> list4 = SgDBUtils.getCLinePitList(uuidJl);
                for (CBasicsWorkEntity entity : list1) {
                    CYbgcInfo info = new CYbgcInfo();
                    info.setUuid(entity.getGuid());
                    info.setTvl2("实有标高：");
                    info.setTvl3("开挖尺寸：");
                    info.setTvl4("埋设深度：");
                    info.setName(entity.getMCName());
                    info.setTvr2(entity.getBWHigh());
                    info.setTvr3(entity.getBWDig());
                    info.setTvr4(entity.getBWDepth());
                    info.setTvr5(entity.getBWResult());
                    info.setTimeAdd(entity.getBWAddtime().getTime());
                    beanList.add(info);
                }
                for (CGroundEntity entity : list2) {
                    CYbgcInfo info = new CYbgcInfo();
                    info.setUuid(entity.getGuid());
                    info.setTvl2("接地体规格：");
                    info.setTvl3("槽沟长度：");
                    info.setTvl4("槽沟深度：");
                    info.setName(entity.getMCName());
                    info.setTvr2(entity.getGFormat());
                    info.setTvr3(entity.getGLength());
                    info.setTvr4(entity.getGDepth());
                    info.setTvr5(entity.getGResult());
                    info.setTimeAdd(entity.getGAddtime().getTime());
                    beanList.add(info);
                }
                for (CPolePitEntity entity : list3) {
                    CYbgcInfo info = new CYbgcInfo();
                    info.setUuid(entity.getGuid());
                    info.setTvl2("实际坑深：");
                    info.setTvl3("土层深度：");
                    info.setTvl4("砂石深度：");
                    info.setName(entity.getMCName());
                    info.setTvr2(entity.getPPDesigndepth());
                    info.setTvr3(entity.getPPSoil());
                    info.setTvr4(entity.getPPStone());
                    info.setTvr5(entity.getPPResult());
                    info.setTimeAdd(entity.getPPAddtime().getTime());
                    beanList.add(info);
                }
                for (CLinePitEntity entity : list4) {
                    CYbgcInfo info = new CYbgcInfo();
                    info.setUuid(entity.getGuid());
                    info.setTvl2("实际坑深：");
                    info.setTvl3("土层深度：");
                    info.setTvl4("砂石深度：");
                    info.setName(entity.getMCName());
                    info.setTvr2(entity.getLPDesigndepth());
                    info.setTvr3(entity.getLPSoil());
                    info.setTvr4(entity.getLPStone());
                    info.setTvr5(entity.getLPResult());
                    info.setTimeAdd(entity.getLPAddtime().getTime());
                    beanList.add(info);
                }
                //依据添加时间排序
                Collections.sort(beanList, new Comparator<CYbgcInfo>() {
                    @Override
                    public int compare(CYbgcInfo o1, CYbgcInfo o2) {
                        if (o1.getTimeAdd() < o2.getTimeAdd()) {
                            return 1;
                        } else if (o1.getTimeAdd() == o2.getTimeAdd()) {
                            return 0;
                        }
                        return -1;
                    }
                });
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
}
